package ink.xiaobaibai.controller.coupon;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ink.xiaobaibai.autoConfig.roleResources.ResourcesRoleMapping;
import ink.xiaobaibai.common.MyRole;
import ink.xiaobaibai.entity.CouponCode;
import ink.xiaobaibai.entity.CouponCodeLog;
import ink.xiaobaibai.response.ResponseFormat;
import ink.xiaobaibai.service.ICouponCodeLogService;
import ink.xiaobaibai.service.ICouponCodeService;
import ink.xiaobaibai.valited.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 优惠券码接口
 * @author: 小白白
 * @create: 2021-08-06
 **/

@Api(tags = "优惠券码接口")
@RestController
@RequestMapping("/coupon-code")
@CrossOrigin
@ResourcesRoleMapping(resourceName = "优惠券操作", includeRoleNames = {MyRole.ADMIN, MyRole.POWER_COUPON})
public class CouponCodeController {

    @Autowired
    private ICouponCodeService couponCodeService;

    @Autowired
    private ICouponCodeLogService couponCodeLogService;

    @GetMapping("/list")
    @ApiOperation("查看所有优惠码")
    public ResponseFormat list(@RequestParam("pageNum") Integer pageNum,
                               @RequestParam("pageSize") Integer pageSize) {
        Page<CouponCode> page = this.couponCodeService.page(new Page<>(pageNum, pageSize));
        return ResponseFormat.success(page);
    }

    @PostMapping("/add")
    @ApiOperation("增加优惠券码 | name活动名称+letter_pre字母前缀(固定6位)+count数量+percentage折扣率+expires_time失效时间+type业务类型0/1 0定点1月费")
    public ResponseFormat addCouponCode(@RequestBody @Validated CouponCode couponCode) {
        QueryWrapper<CouponCode> q1 = new QueryWrapper<>();
        q1.eq("letter_pre", couponCode.getLetterPre());
        if (this.couponCodeService.count(q1) != 0) {
            return ResponseFormat.fail("字母前缀重复,请重新输入");
        }
        this.couponCodeService.addCouponCodeBitMap(couponCode.getLetterPre(), couponCode.getExpiresTime());
        boolean save = this.couponCodeService.save(couponCode);
        return save ? ResponseFormat.success() : ResponseFormat.fail("增加失败");
    }

    @PutMapping("/update")
    @ApiOperation("修改优惠券码 | name+percentage+expires_time 只能改这三个,其他不能改(改了也无效),记得连id一次传入")
    public ResponseFormat updateCouponCode(@RequestBody @Validated(UpdateGroup.class) CouponCode couponCode) {
        couponCode.setCreateTime(null);
        couponCode.setCount(null);
        couponCode.setLetterPre(null);
        couponCode.setType(null);
        CouponCode couponCodeDB = this.couponCodeService.getById(couponCode.getId());
        if (couponCodeDB.getExpiresTime().getTime() < System.currentTimeMillis()) {
            return ResponseFormat.fail("当前优惠券码已经过期,不可修改");
        }
        this.couponCodeService.updateCouponBitMap(couponCode.getLetterPre(), couponCode.getExpiresTime());
        boolean update = this.couponCodeService.updateById(couponCode);
        return update ? ResponseFormat.success() : ResponseFormat.fail("修改失败");
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除当前券码(删除了别人就输入就会说当前券码不存在,就用不了了)")
    public ResponseFormat deleteCouponCode(@PathVariable Integer id) {
        CouponCode couponCode = this.couponCodeService.getById(id);
        boolean remove = this.couponCodeService.removeById(id);
        this.couponCodeService.deleteCouponBit(couponCode.getLetterPre());
        return remove ? ResponseFormat.success() : ResponseFormat.fail("删除失败");
    }

    @GetMapping("/item")
    @ApiOperation("优惠码使用详细")
    public ResponseFormat getItem(@RequestParam("id") Integer id,
                                  @RequestParam("pageNum") Integer pageNum,
                                  @RequestParam("pageSize") Integer pageSize) {
        QueryWrapper<CouponCode> q1 = new QueryWrapper<>();
        q1.select("letter_pre");
        q1.eq("id", id);
        CouponCode couponCode = this.couponCodeService.getOne(q1);
        String letterPre = couponCode.getLetterPre();
        QueryWrapper<CouponCodeLog> q2 = new QueryWrapper<>();
        q2.likeRight("code", letterPre);
        Page<CouponCodeLog> page = this.couponCodeLogService.page(new Page<>(pageNum, pageSize), q2);
        return ResponseFormat.success(page);
    }

}
